/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include "appcategorymanager.h"
#include <QVector>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDBusConnection>
#include <QMap>
#include <QMetaEnum>
#include <QDebug>

Q_GLOBAL_STATIC_WITH_ARGS(QVector<QStringList>, categoriesVector, ({
    { "All" },
    { "Android" },
    { "Network", "network" },
    { "Messaging", "social"},
    { "Audio", "Video", "video" },
    { "Development", "develop" },
    { "Graphics", "graphic" },
    { "Game", "game" },
    { "Office", "Calculator", "Spreadsheet" , "Presentation", "WordProcessor" , "TextEditor", "office" },
    { "Education", "education" },
    { "System", "Settings", "Security", "system" },
    { "safe" }
}));

AppCategoryManager::AppCategoryManager(QObject *parent)
    : QObject{parent}
{
    QDBusConnection::sessionBus().connect("com.kylin.softwarecenter.getsearchresults",
                                          "/com/kylin/softwarecenter/getsearchresults",
                                          "com.kylin.getsearchresults",
                                          "get_app_category_list_signal", this,
                                          SLOT(updateSoftwareStoreAppCategory(QString)));
}

QList<int> AppCategoryManager::matchingAppCategories(const QString &category) const
{
    QList<int> itemCategories;
    for (int i=0; i<categoriesVector->count(); ++i) {
        if (categoriesVector->at(i).contains(category, Qt::CaseInsensitive)) {
            itemCategories.append(i);
            return itemCategories;
        }
    }
    itemCategories.append(UKUI::BaseItem::Others);
    return itemCategories;
}

QString AppCategoryManager::categoryToString(int value)
{
    static QMetaEnum meta = QMetaEnum::fromType<UKUI::BaseItem::Categories>();
    return meta.valueToKey(value);
}

QStringList AppCategoryManager::categoryStringList()
{
    QStringList categoryStrList;
    static QMetaEnum meta = QMetaEnum::fromType<UKUI::BaseItem::Categories>();
    for (int i=0; i<meta.keyCount(); ++i) {
        categoryStrList.append(meta.valueToKey(i));
    }
    return categoryStrList;
}

QList<int> AppCategoryManager::categoryIntList()
{
    QList<int> categoryIntList;
    static QMetaEnum meta = QMetaEnum::fromType<UKUI::BaseItem::Categories>();
    for (int i=0; i<meta.keyCount(); ++i) {
        categoryIntList.append(i);
    }
    return categoryIntList;
}

void AppCategoryManager::updateSoftwareStoreAppCategory(const QString &categray)
{
    QJsonDocument jsonDocument = QJsonDocument::fromJson(categray.toLocal8Bit().data());
    QJsonObject jsonObject = jsonDocument.object();
    QJsonArray jsonArray = jsonObject[jsonObject.keys().constFirst()].toArray();
    Q_FOREACH(QJsonValue jsonValue, jsonArray) {
        QJsonObject arrObject = jsonValue.toObject();
        QString appName = arrObject[arrObject.keys().constFirst()].toString();
        QString appCategory = arrObject[arrObject.keys().at(2)].toString();
        m_softwareAppCategory[appName] = appCategory;
    }
    //与ukui-menu的分类相同
    m_softwareAppCategory["indicator-china-weather"] = "others";
    m_softwareAppCategory["peony"] = "others";
    m_softwareAppCategory["kylin-usb-creator"] = "others";
    m_softwareAppCategory["kylin-burner"] = "others";
    m_softwareAppCategory["sc-reader"] = "office";
    m_softwareAppCategory["kylin-scanner"] = "office";
    m_softwareAppCategory["onboard"] = "system";
    m_softwareAppCategory["evolution"] = "system";
    m_softwareAppCategory["kylin-screenshot"] = "system";
    m_softwareAppCategory["kylin-ipmsg"] = "network";
    m_softwareAppCategory["ksc-defender"] = "safe";
    m_softwareAppCategory["yhkylin-backup-tools"] = "safe";
    m_softwareAppCategory["box-manager"] = "safe";
    m_softwareAppCategory["ukui-system-monitor"] = "safe";

    Q_EMIT softwareAppCategoryUpdated();
}
